package com.jlu.springsecurity.handler;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyAccessDeniedHandler implements AccessDeniedHandler {
  @Override
  public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {

    httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
    httpServletResponse.setHeader("Content-Type","application/json;charset=utf8");
    PrintWriter writer = httpServletResponse.getWriter();
    writer.write("{\"status\":\"error\",\"msg\":\"权限不足\"，\"请联系管理员\"}");
    writer.flush();
    writer.close();
  }
}
